<?php
namespace jy_area_psi;
defined('BY_JYA') or exit('error');
/*
	CV
	
*/
class cv extends \plugin{
	//购物送CV
	public function buygoods($order=array()){
		//file_put_contents(ROOT_D.'/1.txt',111);
		$setting=plugin('jy_yangjidan')->plugin_setting('basic');
		model('jy_yangjidan/model')->upmember($order['mid']);
		if($setting['cv_buyrate']>0){
			$cvfee=$setting['cv_buyrate']*$order['psi'];
			if($cvfee){
				$data=array(
					'uniacid' => $_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'msg'=>'购物奖励'.$cvfee,
					'type'=>0,
					'fee'=>$cvfee,
					'lsfee'=>$cvfee,
					'isadd'=>1,
					'status'=>2,
					'createtime'=>time(),
				);
				pdo_insert('jy_yangjidan_cvlog',$data);
				pdo_update('jy_yangjidan_member',array('cv +='=>$cvfee,'allcv +='=>$cvfee),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		$this->buyji($order,true);
		return true;
	}
	/*
		直推租养奖励CV+小粉丝群购物消费奖CV
	*/
	public function buyji($order=array(),$isgoods=false){
		$isopen=intval($this->setting['isopen']);
		if(!$isopen){
			return;
		}
		$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']),'fromuser');
		if(!$fromuser){
			return;
		}
		$psi_member=pdo_get('jy_area_psi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if(!$psi_member){
			$this->model('member')->register($fromuser);
		}
		$shop_member=pdo_get('jy_weishop_member',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if($shop_member['level']){
			$level=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'shoplevelid'=>intval($shop_member['level'])));
			$rate3=$level['rate3'];
			$rate5=$level['rate5'];
			$rate6=$level['rate6'];
			$rate7=$level['rate7'];
			$rate9=$level['rate9'];
		}else{
			$default_level=$this->plugin_setting('default_level');
			$rate3=$default_level['a0_rate3'];
			$rate5=$default_level['a0_rate5'];
			$rate6=$default_level['a0_rate6'];
			$rate7=$default_level['a0_rate7'];
			$rate9=$default_level['a0_rate9'];
		}
		//直推租养奖励CV
		$cvfee=$rate3*$order['price'];
		if($cvfee && !$isgoods){
			$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$fromuser,
				'msg'=>'直推租养奖励CV'.$cvfee,
				'type'=>3,
				'fee'=>$cvfee,
				'lsfee'=>$cvfee,
				'isadd'=>1,
				'status'=>2,
				'createtime'=>time(),
			);
			pdo_insert('jy_yangjidan_cvlog',$data);
			pdo_update('jy_yangjidan_member',array('cv +='=>$cvfee,'allcv +='=>$cvfee),array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		}
		$setting=plugin('jy_yangjidan')->plugin_setting('basic');
		//小粉丝群蛋鸡消费奖临时CV
		$cvfee=$rate6*$order['price'];
		if($cvfee && !$isgoods){
			$this->xunpai($order,6,$rate6,$fromuser);
		}
		//小粉丝群购物消费奖临时CV
		$cvfee=$rate5*$order['psi'];
		if($cvfee && $isgoods){
			//file_put_contents(ROOT_D.'/1.TXT',93);
			$this->xunpai($order,5,$rate5,$fromuser);
		}
		//直推蛋鸡消费奖临时CV
		$cvfee=$rate9*$order['price'];
		if($cvfee && !$isgoods){
			$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$fromuser,
				'msg'=>'直推蛋鸡消费奖临时CV'.$cvfee,
				'type'=>9,
				'fee'=>$cvfee,
				'lsfee'=>$cvfee,
				'isadd'=>1,
				'status'=>3,
				'endtime'=>time()+$setting['cv_qixian']*60,
				'createtime'=>time(),
			);
			pdo_insert('jy_yangjidan_cvlog',$data);
			pdo_update('jy_yangjidan_member',array('allcv +='=>$cvfee),array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		}
		//私厨成员蛋鸡消费奖佣金
		if(!$isgoods){
			$this->sidai($order);
		}
		return true;
	}
	/*
		循环排位
	*/
	public function xunpai($order=array(),$type=6,$rate=''){
		$setting=plugin('jy_yangjidan')->plugin_setting('basic');
		$member=pdo_get('jy_yangjidan_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		$paimid=$member['paimid'];
		for($i=1;$i<100;$i++){
				if(!$paimid){
					break;
				}
				$allfee0=model('jy_yangjidan/model')->getyeji($paimid,0);
				$allfee1=model('jy_yangjidan/model')->getyeji($paimid,1);
				$chafee=$allfee1>$allfee0?$allfee1-$allfee0:$allfee0-$allfee1;
				if(($allfee0>$allfee1 && $member['isa']) || ($allfee0<$allfee1 && !$member['isa'])){
					if($type==6){
						$jifee=$order['price']>$chafee?$chafee:$order['price'];
						$cvfee=$rate*$jifee;
						$msg='小粉丝群蛋鸡消费奖临时CV'.$cvfee;
					}else{
						$jifee=$order['psi']>$chafee?$chafee:$order['psi'];
						$cvfee=$rate*$jifee;
						$msg='小粉丝群购物消费奖临时CV'.$cvfee;
					}
					if($cvfee>0){
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$paimid,
							'level'=>0,
							'ordersn'=>'',
							'money'=>$cvfee,
							'type'=>$type,
							'createtime'=>TIMESTAMP,
							'status'=>2,
						);
						$this->insert('commission',$new_commission);
						$data=array(
							'uniacid' => $_SESSION['uniacid'],
							'mid'=>$paimid,
							'msg'=>$msg,
							'type'=>$type,
							'fee'=>$cvfee,
							'lsfee'=>$cvfee,
							'isadd'=>1,
							'status'=>3,
							'endtime'=>time()+$setting['cv_qixian']*60,
							'createtime'=>time(),
						);
						pdo_insert('jy_yangjidan_cvlog',$data);
						pdo_update('jy_yangjidan_member',array('allcv +='=>$cvfee),array('mid'=>$paimid,'uniacid'=>$_SESSION['uniacid']));
					}
				}
				$member=pdo_get('jy_yangjidan_member',array('mid'=>$paimid,'uniacid'=>$_SESSION['uniacid']));
				$paimid=$member['paimid'];
		}
		return true;
	}
	/*
		私厨成员蛋鸡消费奖佣金
	*/
	public function sidai($order=array()){
		$simid=$order['mid'];
		for($i=0;$i<=2;$i++){
			if(!$i){
				$psi_member=pdo_get('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$simid));
			}elseif($i==1){
				$psi_member=pdo_get('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$simid));
			}else{
				$psi_member=pdo_get('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$simid));
			}
			if(!$psi_member['simid']){
				return false;
			}
			if(!$i){
				//file_put_contents(ROOT_D.'/1.TXT',$psi_member['simid']);
				$shop_member=pdo_get('jy_weishop_member',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$psi_member['simid']));
			}
			if(!$i && $shop_member['level']){
				$level=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'shoplevelid'=>intval($shop_member['level'])));
				$rate7=$level['rate7'];
				//file_put_contents(ROOT_D.'/1.TXT',$rate7);
			}else{
				$default_level=$this->plugin_setting('default_level');
				if(!$i){
					$rate7=$default_level['a0_rate7'];
				}elseif($i==1){
					$rate7=$default_level['a3_rate7'];
				}else{
					$rate7=$default_level['a2_rate7'];
				}
				//file_put_contents(ROOT_D.'/1.TXT',$rate7);
			}
			if(substr($rate7,-1)=='%'){
				$money=($order['price']*substr($rate7,0,-1))/100;
			}else{
				$money=$rate7;
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$psi_member['simid'],
					'level'=>0,
					'ordersn'=>'',
					'money'=>$money,
					'type'=>7,
					'createtime'=>TIMESTAMP,
					'status'=>1,
				);
				$this->insert('commission',$new_commission);
			}
			$simid=$psi_member['simid'];
		}
		return false;
	}
}